##Author: Jeremy Ko
#Please make sure to important Data1 manually because of different file paths:
Data1 <- read_excel("Data1.xlsx")

#Make sure you install these packages. 
#install.packages("dplyr")
library(dplyr)
#install.packages("ggplot2")
library(ggplot2)
#install.packages("plm")
library(plm)
#install.packages("broom")
library(broom)
#install.packages("gridExtra")
library(gridExtra)

Data1$ND_GAIN <- Data1$ND_GAIN*10
Data1$Year <- as.numeric(Data1$Year)

plots <- lapply(unique(Data1$Country), function(country) {
  data_subset <- subset(Data1, Country == country)
  
  ggplot(data = data_subset, aes(x = Year, y = ND_GAIN, group = 1)) +
    geom_line() +
    labs(title = paste(country),
         x = "Year",
         y = "Climate Readiness") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 8)) +
    geom_rect(data = data_subset[data_subset$Left_Populism == 1, ],
              aes(xmin = Year - 0.5, xmax = Year + 0.5, ymin = -Inf, ymax = Inf),
              fill = "red", alpha = 0.2, inherit.aes = FALSE) +  # Shading for Left Populism
    geom_rect(data = data_subset[data_subset$Right_Populism == 1, ],
              aes(xmin = Year - 0.5, xmax = Year + 0.5, ymin = -Inf, ymax = Inf),
              fill = "blue", alpha = 0.2, inherit.aes = FALSE) +  # Shading for Right Populism
    scale_x_continuous(breaks = seq(1995, 2020, by = 5), 
                       labels = c("1995", "2000", "2005", "2010", "2015", "2020"))
})

combined_plot <- do.call(grid.arrange, c(plots, ncol = 5))

legend_data <- data.frame(
  fill = c("Left Populism", "Right Populism"),
  color = c("red", "blue")
)

legend <- ggplot(legend_data, aes(fill = fill)) + 
  geom_blank() +  
  geom_rect(aes(xmin = 0, xmax = 1, ymin = 0, ymax = 1), alpha = 0.2) +
  scale_fill_manual(name = "Type of Populism", 
                    values = c("Left Populism" = "red", "Right Populism" = "blue")) +
  theme_void() +  
  theme(legend.position = "bottom")

grid.arrange(combined_plot, legend, ncol = 1, heights = c(10, 1))  # Adjust heights as needed

#When Download the Figure, please export as image. I suggest with length and width of around 1200. 





